package io.github.talelin.latticy.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.model.WxUserDO;
import io.github.talelin.latticy.service.ContactsService;
import io.github.talelin.latticy.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import io.github.talelin.latticy.model.ContactsDO;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;

import org.springframework.web.bind.annotation.RestController;

/**
* @author generator@lms
* @since 2021-04-28
*/
@RestController
@RequestMapping("/api/contacts")
@Validated
public class ContactsApiController {

    @Autowired
    private ContactsService contactsService;

    @Autowired
    private WxUserService wxUserService;

    @PostMapping("/addContacts/{id}")
    public CreatedVO addContacts(@RequestParam Integer uid,
                                 @PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        WxUserDO wxUser = wxUserService.getById(id);
        if (wxUser == null) {
            throw new NotFoundException(20000);
        }
        ContactsDO contacts = new ContactsDO();
        contacts.setContactsId(wxUser.getId())
                .setContactsName(wxUser.getUsername())
                .setContactsAvatar(wxUser.getAvatar())
                .setUid(uid);
        contactsService.getBaseMapper().insert(contacts);
        return new CreatedVO("添加成功！");
    }

    @GetMapping("/page")
    public PageResponseVO<ContactsDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam Integer uid
    ) {
        Page<ContactsDO> pager = new Page<>(page, count);
        IPage<ContactsDO> paging = contactsService.getBaseMapper().selectPage(pager, new QueryWrapper<ContactsDO>().eq("uid", uid));
        return PageUtil.build(paging);
    }

}
